<template>
  <div>
    <!-- 搜索框 -->
    <el-card id="search">
      <el-row>
        <el-col :span="20">
          <el-col :span="4.5">
            <el-select
              style="width: 80%"
              clearable
              v-model="searchModel.levelId"
              placeholder="请选择一二级评论"
            >
              <el-option
                v-for="item in commentLevelList"
                :key="item.levelId"
                :label="item.levelName"
                :value="item.levelId"
              >
              </el-option>
            </el-select>
          </el-col>
          <el-col :span="4.5">
            <el-select
              style="width: 80%"
              clearable
              v-model="searchModel.type"
              placeholder="请选择评论事项"
            >
              <el-option
                v-for="item in typeList"
                :key="item.typeId"
                :label="item.typeName"
                :value="item.typeId"
              >
              </el-option>
            </el-select>
          </el-col>
          <el-button
            @click="getList"
            type="primary"
            round
            icon="el-icon-search"
          >查询</el-button
          >
        </el-col>
      </el-row>
    </el-card>
    <!-- 结果表格 -->
    <el-card>
      <el-table :data="dataList" stripe style="width: 100%">
        <el-table-column label="#" width="80">
          <template slot-scope="scope">
            <!-- (pageNo - 1) * pageSize + index + 1 -->
            {{
              (searchModel.pageNo - 1) * searchModel.pageSize + scope.$index + 1
            }}
          </template>
        </el-table-column>
        <el-table-column prop="id" label="评论ID" width="180" align="center">
        </el-table-column>
        <el-table-column prop="nickName" label="评论用户" width="180" align="center">
        </el-table-column>
        <el-table-column prop="thingName" label="评论事物" width="180" align="center">
        </el-table-column>
        <el-table-column prop="commentContent" label="评论内容" width="220" align="center">
        </el-table-column>
        <el-table-column prop="time" label="评论时间" width="180" align="center"> </el-table-column>
        <el-table-column prop="state" label="状态" width="180" align="center">
          <template slot-scope="scope">
            <el-tag type="danger" v-if="scope.row.state == -1">未通过</el-tag>
            <el-tag type="primary" v-if="scope.row.state == 0">未审核</el-tag>
            <el-tag type="success" v-if="scope.row.state == 1">已通过</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="220" align="center">
          <template slot-scope="scope">
            <el-col>
              <el-button
                v-if="scope.row.state == 0"
                @click="editState(scope.row, 1)"
                type="success"
                round
              >审核通过</el-button>
              <el-button
                v-if="scope.row.state == 0"
                @click="editState(scope.row, -1)"
                type="danger"
                round
              >不允通过</el-button>
            </el-col>
            <el-button v-if="scope.row.state == 1" type="success" round>审核成功</el-button>
            <el-button v-if="scope.row.state == -1" type="danger" round>审核不过</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchModel.pageNo"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="searchModel.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

  </div>
</template>

<script>
import commentApi from "@/api/commentManage.js";
import subCommentApi from "@/api/subCommentManage";
export default {
  data() {
    return {
      typeId: '',
      levelId: '',
      typeList: [
        {
          typeId: 1,
          typeName: '新闻'
        },
        {
          typeId: 2,
          typeName: '摊位'
        }
      ],
      commentLevelList: [
        {
          levelId: 1,
          levelName: '一级评论'
        },
        {
          levelId: 2,
          levelName: '二级评论'
        }
      ],
      editorOption:{},
      imageUrl: '',
      uploadUrl: 'http://localhost:8080/common/upload',
      downloadImageUrl: 'http://localhost:8080/common/download?name=',
      formLabelWidth: "130px",
      form: {},
      dialogFormVisible: false,
      title: "",
      total: 0,
      searchModel: {
        pageNo: 1,
        pageSize: 10,
        levelId: 1
      },
      dataList: [],
    };
  },
  methods: {
    editState(row, id) {
      this.$confirm(`确定进行此操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          row.state = id
          if (this.searchModel.levelId == 1){
            commentApi.save(row).then((response) => {
              // 成功提示
              this.$message({
                message: response.message,
                type: "success",
              });
              //刷新表格
              this.getList();
            });
          }
          if (this.searchModel.levelId == 2){
            subCommentApi.save(row).then((response) => {
              // 成功提示
              this.$message({
                message: response.message,
                type: "success",
              });
              //刷新表格
              this.getList();
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    clearForm() {
      this.form = {};
    },
    openEditUI(row) {
      if (row == null) {
        this.title = "新增新闻";
      } else {
        this.title = "修改新闻";
        this.form = row;
      }
      this.dialogFormVisible = true;
    },
    handleSizeChange(pageSize) {
      this.searchModel.pageSize = pageSize;
      this.getList();
    },
    handleCurrentChange(pageNo) {
      this.searchModel.pageNo = pageNo;
      this.getList();
    },
    beforeImageUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传图片只能是 JPG 格式或者是PNG!')
      }

      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    handleImageSuccess(response, file) {
      this.imageUrl =
        'http://localhost:8080/common/download?name=' + response.data
      this.form.coverImage = response.data
      console.log('上传的封面:',this.form.coverImage)
    },
    getList() {
      if (this.searchModel.levelId == 1){
        commentApi.getList(this.searchModel).then((response) => {
          this.dataList = response.data.rows;
          this.total = response.data.total;
        });
      }
      if (this.searchModel.levelId == 2){
        subCommentApi.getList(this.searchModel).then((response) => {
          this.dataList = response.data.rows;
          this.total = response.data.total;
        });
      }
    },
  },
  created() {
    this.getList();
  },
};
</script>

<style>
#search .el-input {
  width: 200px;
  margin-right: 10px;
}
.el-dialog .el-input {
  width: 35%;
}
</style>
